package com.woniu.scss.service.impl;

import com.woniu.common.utils.ResponseData;
import com.woniu.common.utils.SnowflakeIdGenerator;
import com.woniu.scss.dto.ReturnProductTypeDto;
import com.woniu.scss.pojo.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.scss.mapper.ProductTypeMapper;
import com.woniu.scss.pojo.ProductType;
import com.woniu.scss.service.ProductTypeService;
@Service
public class ProductTypeServiceImpl extends ServiceImpl<ProductTypeMapper, ProductType> implements ProductTypeService{
    @Autowired
    private ProductTypeMapper productTypeMapper;
    @Override
    public ResponseData<?> queryAllProductType() {
        List<ReturnProductTypeDto> productTypesQuery = productTypeMapper.queryAllProductType();

        ArrayList<ReturnProductTypeDto> listParent = new ArrayList<>();//父菜单合计
        for (ReturnProductTypeDto rptd : productTypesQuery) {
            if ("-1".equals(rptd.getParentId())){
                ReturnProductTypeDto returnProductTypeDto = new ReturnProductTypeDto();
                returnProductTypeDto.setId(rptd.getId());
                returnProductTypeDto.setTypeName(rptd.getTypeName());
                returnProductTypeDto.setStatus(returnProductTypeDto.getStatus());
                returnProductTypeDto.setChildren(null);
                returnProductTypeDto.setParentId(rptd.getParentId());
                listParent.add(rptd);
            }
        }
        for (ReturnProductTypeDto returnProductTypeDto : listParent) {
            List<ReturnProductTypeDto> returnP = new ArrayList<>();
            for (ReturnProductTypeDto productTypeDto : productTypesQuery) {
                if (!"-1".equals(productTypeDto.getId())  &&  productTypeDto.getParentId().equals(returnProductTypeDto.getId())){
                    returnP.add(productTypeDto);
                }
            }
            returnProductTypeDto.setChildren(returnP);
        }
        return new ResponseData<>().success(listParent);
    }

    @Override
    public ResponseData<?> addNewChild(ReturnProductTypeDto dto) throws InterruptedException {
        ProductType productType = new ProductType();
        SnowflakeIdGenerator snowflakeIdGenerator = new SnowflakeIdGenerator();
        long l = snowflakeIdGenerator.nextId();
        productType.setId(String.valueOf(l));
        productType.setTypeName(dto.getTypeName());
        productType.setParentId(dto.getParentId());
        productType.setStatus(dto.getStatus());
        save(productType);
        return new ResponseData<>().success();
    }

    @Override
    public ResponseData<?> updateType(ReturnProductTypeDto param) {
        lambdaUpdate().eq(ProductType::getId,param.getId())
                .set(ProductType::getTypeName,param.getTypeName())
                .set(ProductType::getStatus,param.getStatus()).update();

        return new ResponseData<>().success();
    }


}
